package org.wms.minbearwcs.serivce.inbound;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wms.minbearwcs.dao.inbound.inf.InboundOrderDao;
import org.wms.minbearwcs.dao.inbound.inf.InboundOrderDetailDao;
import org.wms.minbearwcs.dao.stock.inf.MaterialStockDao;
import org.wms.minbearwcs.entity.DTO.inbound.mes.InboundMesCallAgv;
import org.wms.minbearwcs.entity.DTO.inbound.mission.CallAgvToExchangeArea;
import org.wms.minbearwcs.entity.DTO.inbound.order.InboundMaterialReceive;
import org.wms.minbearwcs.entity.DTO.inbound.order.InboundOrderReceive;
import org.wms.minbearwcs.entity.DTO.inbound.order.InboundPalletReceive;
import org.wms.minbearwcs.entity.DTO.transfer.Tuple;
import org.wms.minbearwcs.entity.PO.wcs.inbound.InboundOrder;
import org.wms.minbearwcs.entity.PO.wcs.inbound.InboundOrderDetail;
import org.wms.minbearwcs.entity.PO.wcs.inbound.status.InboundOrderDetailStatus;
import org.wms.minbearwcs.entity.PO.wcs.inbound.status.InboundOrderStatus;
import org.wms.minbearwcs.entity.PO.wcs.stock.MaterialStock;
import org.wms.minbearwcs.serivce.mission.generate.MissionGeneratorCollector;
import org.wms.minbearwcs.serivce.mission.generate.type.MissionGenType;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class InboundService {

    private final InboundOrderDao inboundOrderDao;
    private final InboundOrderDetailDao inboundOrderDetailDao;
    private final MaterialStockDao materialStockDao;
    private final MissionGeneratorCollector missionGeneratorCollector;


    @Transactional
    public boolean receiveInboundOrder(InboundOrderReceive receive) {

        InboundOrder inboundOrder = new InboundOrder(null, receive.getInboundOrderCode(), receive.getPriority(),
                InboundOrderStatus.READY, LocalDateTime.now(), null, null);
        inboundOrderDao.insert(inboundOrder);

        for (InboundPalletReceive palletReceive : receive.getPalletList()) {
            InboundOrderDetail inboundOrderDetail = new InboundOrderDetail(null, receive.getInboundOrderCode(), palletReceive.getPalletCode(),
                    InboundOrderDetailStatus.WAIT_FOR_CALL_AGV, null,null,null,null);
            inboundOrderDetailDao.insert(inboundOrderDetail);

            for (InboundMaterialReceive materialReceive : palletReceive.getMaterialList()) {
                MaterialStock materialStock = new MaterialStock(null, materialReceive.getMaterialSerialCode(), materialReceive.getMaterialCode(),
                        materialReceive.getBoxCode(), palletReceive.getPalletCode(), receive.getInboundOrderCode(),
                        null, materialReceive.getProductionDate(), materialReceive.getProductionLine());

                materialStockDao.insert(materialStock);
            }
        }

        return true;
    }


    public Tuple<String> receiveMesCallAgv(InboundMesCallAgv inboundMesCallAgv) {
        InboundOrderDetail inboundOrderDetail = inboundOrderDetailDao.selectDetailByOrderCodeAndPalletCode(
                inboundMesCallAgv.getInboundOrderCode(),
                inboundMesCallAgv.getPalletCode(),
                InboundOrderDetailStatus.WAIT_FOR_CALL_AGV
        );

        if (inboundOrderDetail == null) {
            return Tuple.tuple("任务已开始或者不存在",false);
        }

        inboundOrderDetailDao.updateStartPointById(inboundOrderDetail.getId(), inboundMesCallAgv.getStartPoint());

        CallAgvToExchangeArea callAgvToExchangeArea = new CallAgvToExchangeArea(inboundOrderDetail.getInboundOrderCode(),
                inboundMesCallAgv.getPalletCode(),
                inboundMesCallAgv.getStartPoint(),
                1,
                inboundOrderDetail.getId());
        return missionGeneratorCollector.getGenerator(MissionGenType.INBOUND).generate(callAgvToExchangeArea);
    }

}
